package com.qzsoft.zhjd.cement.modules.cementmatch.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.cement.modules.cementmatch.dto.CementMatchDetailsDto;
import com.qzsoft.zhjd.cement.modules.cementmatch.dto.CementMatchListDto;
import com.qzsoft.zhjd.cement.modules.cementmatch.entity.CementMatchEntity;
import com.qzsoft.zhjd.cement.modules.cementmatch.service.CementMatchService;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;

/**
 * @ClassName CementMatchController
 * @Description 水稳级配控制器
 * @author linhuawei
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/api/cementMatch")
public class CementMatchController {

    @Autowired
    private CementMatchService cementMatchService;

    /**
     * @Title page
     * @Description 生产配比分页列表
     * @param params 查询条件
     * @return 生产配比分页列表
     */
    @RequiresPermissions("cement:match:page")
    @GetMapping("/page")
    public Result<IPage<CementMatchListDto>> page(@RequestParam Map<String, Object> params) {
        IPage<CementMatchListDto> page = cementMatchService.pageDto(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title list
     * @Description 获取生产配比列表
     * @param params 查询条件
     * @return 生产配比列表
     */
    @GetMapping("/list")
    public Result<List<CementMatchEntity>> list(@RequestParam Map<String, Object> params) {
        List<CementMatchEntity> list = cementMatchService.list(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title info
     * @Description 生产配比详情
     * @param id 主键
     * @return 生产配比
     */
    @RequiresPermissions(value = { "cement:match:update", "cement:match:info" }, logical = Logical.OR)
    @GetMapping("/info/{id}")
    public Result<CementMatchEntity> info(@PathVariable String id) {
        CementMatchEntity entity = cementMatchService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND, "生产配比不存在");
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title details
     * @Description 生产配比详情
     * @param id 主键
     * @return 生产配比
     */
    @RequiresPermissions("cement:match:info")
    @GetMapping("/details/{id}")
    public Result<CementMatchDetailsDto> details(@PathVariable String id) {
        CementMatchDetailsDto data = cementMatchService.getDetails(id);
        if (null == data) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND, "生产配比不存在");
        }
        return ResultUtils.ok(data);
    }

    /**
     * @Title save
     * @Description 添加生产配比
     * @param entity 生产配比
     * @return 添加结果
     */
    @RequiresPermissions("cement:match:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody CementMatchEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        cementMatchService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改生产配比
     * @param entity 生产配比
     * @return 修改结果
     */
    @RequiresPermissions("cement:match:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody CementMatchEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        cementMatchService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除水稳级配
     * @param id 级配主键
     * @return 删除结果
     */
    @RequiresPermissions("cement:match:delete")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable String id) {
        cementMatchService.removeById(id);
        return ResultUtils.ok();
    }

}
